<template>
  <el-card class="content">
    <el-form :model="query" ref="formRef" :inline="true">
      <el-form-item label="开始时间:">
        <el-date-picker
          type="datetime"
          placeholder="选择开始日期"
          default-time="00:00:00"
          size="mini"
          v-model="startTime"
          :picker-options="pickerOptions0"
          clearable
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间:">
        <el-date-picker
          type="datetime"
          placeholder="选择结束日期"
          default-time="00:00:00"
          size="mini"
          v-model="endTime"
          :picker-options="pickerOptions0"
          clearable
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="统计时间类型:" v-if="this.query.monitorTypeId == 3">
        <el-select v-model="query.timeType" size="mini">
          <el-option label="动态10分钟" value="10"></el-option>
          <el-option label="动态1小时" value="1"></el-option>
          <el-option label="动态2小时" value="2"></el-option>
          <el-option label="动态6小时" value="6"></el-option>
          <el-option label="动态24小时" value="24"></el-option>
          <el-option label="静态10分钟" value="s_1h"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="统计时间类型:" v-if="this.query.monitorTypeId == 4">
        <el-select v-model="query.timeType" size="mini">
          <el-option label="每1小时" value="1"></el-option>
          <el-option label="每24小时" value="24"></el-option>
          <el-option label="每10分钟" value="10"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="统计时间类型:" v-if="this.query.monitorTypeId == 6">
        <el-select v-model="query.timeType" size="mini">
          <el-option label="1小时" value="1"></el-option>
          <el-option label="1天" value="24"></el-option>
          <el-option label="1周" value="7"></el-option>
          <el-option label="1月" value="30"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          size="mini"
          type="primary"
          @click="getMonitoringDataStatisticsAll"
          >查询</el-button
        >
      </el-form-item>
      <br />
      <el-form-item label="监测类型:">
        <el-select
          v-model="query.monitorTypeId"
          placeholder="请选择监测类型"
          @change="selectGoodsByGroupId($event)"
          size="mini"
        >
          <el-option
            v-for="item in monitorTypeList"
            :key="item.monitorTypeId"
            :value="item.monitorTypeId"
            :label="item.typeName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="站点名称:">
        <el-select
          v-model="query.monitorId"
          clearable
          placeholder="请选择站点名称"
          size="mini"
          @change="changeCharts($event)"
        >
          <el-option
            v-for="item in monitorList"
            :key="item.monitorId"
            :value="item.monitorId"
            :label="item.monitorName"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-table
      v-show="
        this.query.monitorTypeId == 1 ||
        this.query.monitorTypeId == 2 ||
        this.query.monitorTypeId == 5 ||
        this.query.monitorTypeId == null
      "
      :data="tableData"
      class="el-table-box"
      stripe
      size="small"
      :row-style="{ height: '.255rem' }"
    >
      <el-table-column label="测点名称" prop="monitorName" align="center">
      </el-table-column>
      <el-table-column prop="monitorCode" label="测点编号" align="center">
      </el-table-column>
      <el-table-column prop="data" label="数值(m)" align="center">
      </el-table-column>
      <el-table-column prop="theDate" label="数据时间" align="center">
      </el-table-column>
    </el-table>
    <el-table
      v-show="this.query.monitorTypeId == 3"
      :data="tableData"
      class="el-table-box"
      stripe
      size="small"
      :row-style="{ height: '.255rem' }"
    >
      <el-table-column
        label="测点名称"
        prop="monitorName"
        align="center"
      ></el-table-column>
      <el-table-column
        label="WGS84-H(m)"
        prop="dataH"
        align="center"
      ></el-table-column>
      <el-table-column
        label="WGS84-x(m)"
        prop="dataGx"
        align="center"
      ></el-table-column>
      <el-table-column
        label="WGS84-y(m)"
        prop="dataGy"
        align="center"
      ></el-table-column>
      <el-table-column
        label="WGS84-ΔH(mm)"
        prop="totalH"
        align="center"
      ></el-table-column>
      <el-table-column
        label="WGS84-ΔP(mm)"
        prop="totalP"
        align="center"
      ></el-table-column>
      <el-table-column
        label="WGS84-ΔX(mm)"
        prop="totalX"
        align="center"
      ></el-table-column>
      <el-table-column
        label="WGS84-ΔY(mm)"
        prop="totalY"
        align="center"
      ></el-table-column>
      <el-table-column
        label="WGS84-VH(mm)"
        prop="ghSpeed"
        align="center"
      ></el-table-column>
      <el-table-column
        label="WGS84-Vx(mm)"
        prop="gxSpeed"
        align="center"
      ></el-table-column>
      <el-table-column
        label="WGS84-Vy(mm)"
        prop="gySpeed"
        align="center"
      ></el-table-column>
      <el-table-column
        label="统计数量"
        prop="dataCount"
        align="center"
      ></el-table-column>
      <el-table-column
        label="统计时间"
        prop="theDate"
        align="center"
        width="150"
      ></el-table-column>
    </el-table>
    <el-table
      v-show="this.query.monitorTypeId == 4"
      :data="tableData"
      class="el-table-box"
      stripe
      size="small"
      :row-style="{ height: '.255rem' }"
    >
      <el-table-column
        label="测点名称"
        prop="monitorName"
        align="center"
      ></el-table-column>
      <el-table-column
        label="测点编号"
        prop="monitorCode"
        align="center"
      ></el-table-column>
      <el-table-column
        label="通道号"
        prop="tunnel"
        align="center"
      ></el-table-column>
      <el-table-column
        label="x数值(mm)"
        prop="xdata"
        align="center"
      ></el-table-column>
      <el-table-column
        label="数据时间"
        prop="theDate"
        align="center"
      ></el-table-column>
    </el-table>
    <el-table
      v-show="this.query.monitorTypeId == 6"
      :data="tableData"
      class="el-table-box"
      stripe
      size="small"
      :row-style="{ height: '.255rem' }"
    >
      <el-table-column
        label="测点名称"
        prop="monitorName"
        align="center"
      ></el-table-column>
      <el-table-column
        label="测点编号"
        prop="monitorCode"
        align="center"
      ></el-table-column>
      <el-table-column
        label="数值(mm)"
        prop="data"
        align="center"
      ></el-table-column>
      <el-table-column
        label="数据时间"
        prop="theDate"
        align="center"
      ></el-table-column>
    </el-table>
    <el-row type="flex" justify="end">
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 15]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>
  </el-card>
</template>
<script>
import moment from "moment";
import {
  getAllMonitoringAndMonitoringType,
  getMonitoringDataStatisticsAll,
} from "../../../api/api";
export default {
  name: "DataStatistics",
  props: {
    node: Object,
  },
  data() {
    return {
      query: {
        page: 1,
        pagesize: 10,
        timeType: "",
        monitorTypeId: null,
        monitorId: null,
      },
      pickerOptions0: {
        disabledDate(time) {
          return time.getTime() > Date.now(); //如果没有后面的-8.64e6就是不可以选择今天的
        },
      },
      startTime: moment().subtract(1, "month").format("YYYY-MM-DD 00:00:00"),
      endTime: moment().format("YYYY-MM-DD HH:mm:ss"),
      monitorTypeList: [],
      monitorList: [],
      tableData: [],
      total: null,
    };
  },
  mounted() {
    this.getAllMonitoringAndMonitoringType();
  },
  methods: {
    // 获取监测类型及监测
    async getAllMonitoringAndMonitoringType() {
      const res = await getAllMonitoringAndMonitoringType({
        tailingPondId: sessionStorage.getItem("sensorId"),
      });
      if (res.resultCode == "200") {
        this.monitorTypeList = res.parameter;
        this.query.monitorTypeId = this.monitorTypeList[0].monitorTypeId;
        this.query.monitorId =
          this.monitorTypeList[0].monitoringList[0].monitorId;
        this.getMonitoringDataStatisticsAll();
        this.selectGoodsByGroupId(this.query.monitorTypeId);
      } else {
        this.$message({
          message: "暂无数据",
          offset: 400,
          duration: 1000,
        });
      }
    },
    changeCharts() {
      this.tableData = [];
      this.getMonitoringDataStatisticsAll();
    },
    selectGoodsByGroupId(val) {
      this.monitorList = [];
      this.tableData = [];
      this.total = 0;
      // this.query.monitorId = "";
      if (this.query.monitorTypeId == 3) {
        this.query.timeType = "s_1h";
      } else if (this.query.monitorTypeId == 4) {
        this.query.timeType = "10m";
      } else if (this.query.monitorTypeId == 6) {
        this.query.timeType = "1h";
      } else {
        this.query.timeType = null;
      }
      for (let i = 0; i < this.monitorTypeList.length; i++) {
        if (
          val == this.monitorTypeList[i].monitorTypeId &&
          val != null &&
          val != "" &&
          val != undefined
        ) {
          for (
            let j = 0;
            j < this.monitorTypeList[i].monitoringList.length;
            j++
          ) {
            if (this.monitorTypeList[i].monitoringList != null) {
              this.monitorList.push(this.monitorTypeList[i].monitoringList[j]);
            } else {
              this.monitorList = [];
            }
          }
          this.query.monitorId = this.monitorList[0].monitorId;
        }
      }
      this.getMonitoringDataStatisticsAll();
    },
    async getMonitoringDataStatisticsAll() {
      let queryData = {
        startTime: moment(this.startTime).format("YYYY-MM-DD 00:00:00"),
        endTime: moment(this.endTime).format("YYYY-MM-DD HH:mm:ss"),
      };
      const res = await getMonitoringDataStatisticsAll({
        startTime: queryData.startTime,
        endTime: queryData.endTime,
        monitorTypeId: this.query.monitorTypeId,
        monitorId: this.query.monitorId,
        timeType: this.query.timeType,
        pageCount: this.query.page,
        pageSize: this.query.pagesize,
      });
      if (res.resultCode == "200") {
        this.tableData = res.parameter.list.list;
        this.total = res.parameter.list.total;
        if (this.query.monitorTypeId == 3) {
          this.tableData.forEach((item) => {
            item.dataH = Number(item.dataH).toFixed(2);
            item.dataGx = Number(item.dataGx).toFixed(2);
            item.dataGy = Number(item.dataGy).toFixed(2);
            item.totalH = Number(item.totalH).toFixed(2);
            item.totalY = Number(item.totalY).toFixed(2);
            item.totalX = Number(item.totalX).toFixed(2);
            item.totalP = Number(item.totalP).toFixed(2);
            item.gySpeed = Number(item.gySpeed).toFixed(2);
            item.ghSpeed = Number(item.ghSpeed).toFixed(2);
            item.gxSpeed = Number(item.gxSpeed).toFixed(2);
          });
        } else if (this.query.monitorTypeId == 4) {
          this.tableData.forEach((item) => {
            item.xdata = item.xdata.toFixed(2);
          });
        } else {
          this.tableData.forEach((item) => {
            item.data = item.data.toFixed(2);
          });
        }
      } else {
        this.tableData = [];
        this.total = 0;
        this.$message({
          message: "暂无数据",
          offset: 400,
          duration: 1000,
        });
      }
    },
    handleSizeChange(val) {
      this.query.pagesize = val;
      this.getMonitoringDataStatisticsAll();
    },
    handleCurrentChange(val) {
      this.query.page = val;
      this.getMonitoringDataStatisticsAll();
    },
  },
};
</script>
<style scoped>
.content {
  margin-left: 0.08rem;
  font-size: 0.07rem;
  height: 87vh;
}
.el-table-box {
  margin-bottom: 0.1rem;
  display: block;
  height: 60vh;
  overflow: auto;
}
.el-table::before {
  z-index: inherit;
}
</style>